package com.expense.common.util;

import cn.hutool.core.util.StrUtil;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;

public class MoneyUtil {
  /**
   * 汉语中数字大写
   */
  private static final String[] CN_UPPER_NUMBER = {"零", "壹", "贰", "叁", "肆",
      "伍", "陆", "柒", "捌", "玖"};

  /**
   * 汉语中货币单位大写，这样的设计类似于占位符
   */
  private static final String[] CN_UPPER_MONETRAY_UNIT = {"分", "角", "元",
      "拾", "佰", "仟", "万", "拾", "佰", "仟", "亿", "拾", "佰", "仟", "兆", "拾",
      "佰", "仟"};
  /**
   * 特殊字符：整
   */
  private static final String CN_FULL = "整";
  /**
   * 特殊字符：负
   */
  private static final String CN_NEGATIVE = "负";
  /**
   * 金额的精度，默认值为2
   */
  private static final int MONEY_PRECISION = 2;
  /**
   * 特殊字符：零元整
   */
  private static final String CN_ZEOR_FULL = "零元" + CN_FULL;

  public  static  String numberToChinese(int num){
    if(num == 0){
      return "零";
    }

    int weigth = 0;//节权位
    String chinese="";
    String chinese_section = "";
    boolean setZero =false;//下一小节是否需要零，第一次没有上一小节所以为false
    while (num >0){
      int section = num%10000;//得到最后面的小节
      if(setZero){//判断上一小节的千位是否为零，是就设置零
        chinese  = TooltoCh(0)+chinese;
      }
      chinese_section = sectionTrans(section);
      if(section != 0){//判断是都加节权位
        chinese_section = chinese_section +getWeight(weigth);
      }

      chinese = chinese_section+chinese;

      chinese_section = "";
      setZero = (section<1000)&&(section>0);
      num = num/10000;
      weigth++;

    }
    if((chinese.length()==2||(chinese.length() == 3))&&chinese.contains("一十")){
      chinese =  chinese.substring(1,chinese.length());
    }
    return chinese;
  }

  public  static  String sectionTrans(int section){
    StringBuilder section_chinese =
        new StringBuilder();
    int pos = 0;//小节内部权位的计数器
    boolean zero = true;//小节内部的置零判断，每一个小节只能有一个零。
    while (section >0){
      int v = section%10;//得到最后一个数
      if(v ==0 ){
        if(!zero){
          zero = true;//需要补零的操作，确保对连续多个零只是输出一个
          section_chinese.insert(0,TooltoCh(0));
        }
      }else {
        zero = false;//有非零数字就把置零打开
        section_chinese.insert(0,getPos(pos));
        section_chinese.insert(0,TooltoCh(v));
      }
      pos++;
      section = section/10;
    }

    return section_chinese.toString();
  }

  public  static String TooltoCh(int num){
    switch (num){
      case 0:
        return "零";
      case 1:
        return "一";
      case 2:
        return "二";
      case 3:
        return "三";
      case 4:
        return "四";
      case 5:
        return "五";
      case 6:
        return "六";
      case 7:
        return "七";
      case 8:
        return "八";
      case 9:
        return "九";
    }
    return "erro";
  }

  public  static String getPos(int pos){
    switch (pos){
      case 1:
        return "十";
      case 2:
        return "百";
      case 3:
        return "千";
    }
    return "";
  }

  public  static  String getWeight(int weight){
    switch (weight){
      case 1:
        return "万";
      case 2:
        return "亿";
    }
    return "";
  }

  /**
   * 把输入的金额转换为汉语中人民币的大写
   *
   * @param numberOfMoney 输入的金额
   * @return 对应的汉语大写
   */
  public static String number2CNMonetrayUnit(BigDecimal numberOfMoney) {
    StringBuffer sb = new StringBuffer();
    // -1, 0, or 1 as the value of this BigDecimal is negative, zero, or
    // positive.
    int signum = numberOfMoney.signum();
    // 零元整的情况
    if (signum == 0) {
      return CN_ZEOR_FULL;
    }
    //这里会进行金额的四舍五入
    long number = numberOfMoney.movePointRight(MONEY_PRECISION)
        .setScale(0, 4).abs().longValue();
    // 得到小数点后两位值
    long scale = number % 100;
    int numUnit = 0;
    int numIndex = 0;
    boolean getZero = false;
    // 判断最后两位数，一共有四中情况：00 = 0, 01 = 1, 10, 11
    if (!(scale > 0)) {
      numIndex = 2;
      number = number / 100;
      getZero = true;
    }
    if ((scale > 0) && (!(scale % 10 > 0))) {
      numIndex = 1;
      number = number / 10;
      getZero = true;
    }
    int zeroSize = 0;
    while (true) {
      if (number <= 0) {
        break;
      }
      // 每次获取到最后一个数
      numUnit = (int) (number % 10);
      if (numUnit > 0) {
        if ((numIndex == 9) && (zeroSize >= 3)) {
          sb.insert(0, CN_UPPER_MONETRAY_UNIT[6]);
        }
        if ((numIndex == 13) && (zeroSize >= 3)) {
          sb.insert(0, CN_UPPER_MONETRAY_UNIT[10]);
        }
        sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
        sb.insert(0, CN_UPPER_NUMBER[numUnit]);
        getZero = false;
        zeroSize = 0;
      } else {
        ++zeroSize;
        if (!(getZero)) {
          sb.insert(0, CN_UPPER_NUMBER[numUnit]);
        }
        if (numIndex == 2) {
          if (number > 0) {
            sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
          }
        } else if (((numIndex - 2) % 4 == 0) && (number % 1000 > 0)) {
          sb.insert(0, CN_UPPER_MONETRAY_UNIT[numIndex]);
        }
        getZero = true;
      }
      // 让number每次都去掉最后一个数
      number = number / 10;
      ++numIndex;
    }
    // 如果signum == -1，则说明输入的数字为负数，就在最前面追加特殊字符：负
    if (signum == -1) {
      sb.insert(0, CN_NEGATIVE);
    }
    // 输入的数字小数点后两位为"00"的情况，则要在最后追加特殊字符：整
    if (!(scale > 0)) {
      sb.append(CN_FULL);
    }
    return sb.toString();
  }

}
